package com.asset.dispoal.poi.documentation.service.commom.util;

import tech.mhuang.pacebox.core.date.DateTimeUtil;
import tech.mhuang.pacebox.springboot.core.util.IdCardUtil;

import java.time.LocalDate;
import java.util.Date;
import java.util.Objects;

public class IdcardProUtil extends IdCardUtil {
    public static int getSexFromIdCard(String idCard) {
        int sex = 9;
        // 身份证号码为空
        if (Objects.equals(idCard, "") || idCard.isEmpty()) {
            return 0;
        }
        if (idCard.length() == 18) {
            if (Integer.parseInt(idCard.substring(16).substring(0, 1)) % 2 == 0) {
                // 判断性别
                sex = 2;
                // 女
            } else {
                sex = 1;
                // 男
            }
        } else if (idCard.length() == 15) {
            String uses = idCard.substring(14, 15);
            // 用户的性别
            if (Integer.parseInt(uses) % 2 == 0) {
                sex = 2;
                // 女
            } else {
                sex = 1;
                // 男
            }
        }
        return sex;
    }

    public static Date getBirthDayFromIdCard(String idCard) {
        Date birth = null;
        if (idCard.length() == 18) {
            String year = idCard.substring(6).substring(0, 4);// 得到年份
            String month = idCard.substring(10).substring(0, 2);// 得到月份
            String day = idCard.substring(12).substring(0, 2);// 得到日
            birth = DateTimeUtil.localDateToStartDate(LocalDate.of(Integer.parseInt(year), Integer.parseInt(month), Integer.parseInt(day)));
        } else if (idCard.length() == 15) {
            String year = "19" + idCard.substring(6, 8);// 年份
            String month = idCard.substring(8, 10);// 月份
            String day = idCard.substring(10, 12);// 得到日
            birth = DateTimeUtil.localDateToStartDate(LocalDate.of(Integer.parseInt(year), Integer.parseInt(month), Integer.parseInt(day)));
        }
        return birth;
    }

}
